package com.wisdomparking.tools;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// import com.mysql.fabric.xmlrpc.base.Data;
import com.wisdomparking.constant.Constants;





/**
 * 日期时间相关
 */
public abstract class ToolDateTime {

	private final static Logger LOG = LoggerFactory.getLogger(ToolDateTime.class);

	public static final String pattern_ymd = "yyyy-MM-dd"; // pattern_ymd
	public static final String pattern_ymd_hms = "yyyy-MM-dd HH:mm:ss"; // pattern_ymdtime
	public static final String pattern_ymd_hms_s = "yyyy-MM-dd HH:mm:ss:SSS"; // pattern_ymdtimeMillisecond

	/**
	 * 主要是给jfinal使用，数据库只认java.sql.*
	 * 
	 * @return
	 */
	public static Timestamp getSqlTimestamp() {
		return getSqlTimestamp(new Date().getTime());
	}
	
	/**
	 * 主要是给动态sql 使用，模板传递String 类型
	 * 
	 * @return
	 */
	public static String getSqlTimestampStr() {
		return getSqlTimestamp(new Date().getTime()).toString();
	}

	/**
	 * 主要是给jfinal使用，数据库只认java.sql.*
	 * 
	 * @param date
	 * @return
	 */
	public static Timestamp getSqlTimestamp(Date date) {
		if (null == date) {
			return getSqlTimestamp();
		}
		return getSqlTimestamp(date.getTime());
	}
	
	public static String getSqlTimestampStr(Date date) {
		if (null == date) {
			return null;
		}
		return getSqlTimestamp(date.getTime()).toString();
	}
	
	public static String getSqlTimestampStr(String date) {
		if (null == date) {
			return null;
		}
		return getSqlTimestamp(date,"yyyy-MM-dd HH:mm:ss").toString();
	}

	/**
	 * 主要是给jfinal使用，数据库只认java.sql.*
	 * 
	 * @param time
	 * @return
	 */
	public static Timestamp getSqlTimestamp(long time) {
		return new java.sql.Timestamp(time);
	}

	/**
	 * 主要是给jfinal使用，数据库只认java.sql.*
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Timestamp getSqlTimestamp(String date, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		try {
			return new java.sql.Timestamp(format.parse(date).getTime());
		} catch (ParseException e) {
			LOG.error("ToolDateTime.parse异常：date值" + date + "，pattern值" + pattern);
			return null;
		}
	}

	/**
	 * 获取当前时间
	 * 
	 * @return
	 */
	public static Date getDate() {
		return new Date();
	}

	/**
	 * 获取指定日期
	 * 
	 * @param date
	 * @param hour
	 * @param minute
	 * @param second
	 * @param millisecond
	 * @return
	 */
	public static Date getDate(int date, int hour, int minute, int second, int millisecond) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, date);
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MILLISECOND, millisecond);
		return calendar.getTime();
	}

	/**
	 * 获取当前时间的时间戳
	 * 
	 * @return
	 */
	public static long getDateByTime() {
		return new Date().getTime();
	}

	/**
	 * 格式化
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String format(Date date, String pattern) {
		DateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	/**
	 * 格式化
	 * 
	 * @param date
	 * @param parsePattern
	 * @param returnPattern
	 * @return
	 */
	public static String format(String date, String parsePattern, String returnPattern) {
		return format(parse(date, parsePattern), returnPattern);
	}

	/**
	 * 解析
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Date parse(String date, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		try {
			return format.parse(date);
		} catch (ParseException e) {
			LOG.error("ToolDateTime.parse异常：date值" + date + "，pattern值" + pattern);
			return null;
		}
	}

	/**
	 * 解析
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parse(String dateStr) {
		Date date = null;
		try {
			date = DateFormat.getDateTimeInstance().parse(dateStr);
		} catch (ParseException e) {
			LOG.error("ToolDateTime.parse异常：date值" + date);
			return null;
		}
		return date;
	}

	/**
	 * 两个日期的时间差，返回"X天X小时X分X秒"
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static String getBetween(Date start, Date end) {
		long between = (end.getTime() - start.getTime()) / 1000;// 除以1000是为了转换成秒
		long day = between / (24 * 3600);
		long hour = between % (24 * 3600) / 3600;
		long minute = between % 3600 / 60;
		long second = between % 60 / 60;

		StringBuilder sb = new StringBuilder();
		sb.append(day);
		sb.append("天");
		sb.append(hour);
		sb.append("小时");
		sb.append(minute);
		sb.append("分");
		sb.append(second);
		sb.append("秒");

		return sb.toString();
	}

	/**
	 * 返回两个日期之间隔了多少分钟
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static int getDateMinuteSpace(Date start, Date end) {
		int hour = (int) ((end.getTime() - start.getTime()) / (60 * 1000));
		return hour;
	}

	/**
	 * 返回两个日期之间隔了多少小时
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static int getDateHourSpace(Date start, Date end) {
		int hour = (int) ((end.getTime() - start.getTime()) / (60 * 60 * 1000));
		return hour;
	}

	/**
	 * 返回两个日期之间隔了多少天
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static int getDateDaySpace(Date start, Date end) {
		int day = (int) ((end.getTime() - start.getTime()) / (60 * 60 * 24 * 1000));
		return day;
	}
	
	
	
	/**
	 * 得到某一天是星期几
	 * 
	 * @param Date 日期字符串
	 * @return String 星期几
	 */
	@SuppressWarnings("static-access")
	public static String getDateInWeek(Date date) {
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int dayIndex = calendar.get(calendar.DAY_OF_WEEK) - calendar.SUNDAY;
		if (dayIndex < 0) {
			dayIndex = 0;
		}
		return weekDays[dayIndex];
	}

	/**
	 * 日期减去多少个小时
	 * 
	 * @param date
	 * @param hourCount
	 *            多少个小时
	 * @return
	 */
	public static Date getDateReduceHour(Date date, long hourCount) {
		long time = date.getTime() - 3600 * 1000 * hourCount;
		Date dateTemp = new Date();
		dateTemp.setTime(time);
		return dateTemp;
	}

	/**
	 * 日期区间分割
	 * 
	 * @param start
	 * @param end
	 * @param splitCount
	 * @return
	 */
	public static List<Date> getDateSplit(Date start, Date end, long splitCount) {
		long startTime = start.getTime();
		long endTime = end.getTime();
		long between = endTime - startTime;

		long count = splitCount - 1l;
		long section = between / count;

		List<Date> list = new ArrayList<Date>();
		list.add(start);

		for (long i = 1l; i < count; i++) {
			long time = startTime + section * i;
			Date date = new Date();
			date.setTime(time);
			list.add(date);
		}

		list.add(end);

		return list;
	}

	/**
	 * 返回两个日期之间隔了多少天，包含开始、结束时间
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static List<String> getDaySpaceDate(Date start, Date end) {
		Calendar fromCalendar = Calendar.getInstance();
		fromCalendar.setTime(start);
		fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
		fromCalendar.set(Calendar.MINUTE, 0);
		fromCalendar.set(Calendar.SECOND, 0);
		fromCalendar.set(Calendar.MILLISECOND, 0);

		Calendar toCalendar = Calendar.getInstance();
		toCalendar.setTime(end);
		toCalendar.set(Calendar.HOUR_OF_DAY, 0);
		toCalendar.set(Calendar.MINUTE, 0);
		toCalendar.set(Calendar.SECOND, 0);
		toCalendar.set(Calendar.MILLISECOND, 0);

		List<String> dateList = new LinkedList<String>();

		long dayCount = (toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24);
		if (dayCount < 0) {
			return dateList;
		}

		dateList.add(format(fromCalendar.getTime(), pattern_ymd));

		for (int i = 0; i < dayCount; i++) {
			fromCalendar.add(Calendar.DATE, 1);// 增加一天
			dateList.add(format(fromCalendar.getTime(), pattern_ymd));
		}

		return dateList;
	}

	/**
	 * 获取开始时间
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static Date startDateByDay(Date start, int end) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(start);
		calendar.add(Calendar.DATE, end);// 明天1，昨天-1
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		Date date = calendar.getTime();
		return date;
	}

	/**
	 * 获取结束时间
	 * 
	 * @param start
	 * @return
	 */
	public static Date endDateByDay(Date start) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(start);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		Date date = calendar.getTime();
		return date;
	}

	/**
	 * 获取开始时间
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static Date startDateByHour(Date start, int end) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(start);
		calendar.set(Calendar.MINUTE, end);
		Date date = calendar.getTime();
		return date;
	}

	/**
	 * 获取结束时间
	 * 
	 * @param end
	 * @return
	 */
	public static Date endDateByHour(Date end) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(end);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		Date date = calendar.getTime();
		return date;
	}

	/**
	 * 根据年份和周得到周的开始和结束日期
	 * 
	 * @param year
	 * @param week
	 * @return
	 */
	public static Map<String, Date> getStartEndDateByWeek(int year, int week) {
		Calendar weekCalendar = new GregorianCalendar();
		weekCalendar.set(Calendar.YEAR, year);
		weekCalendar.set(Calendar.WEEK_OF_YEAR, week);
		weekCalendar.set(Calendar.DAY_OF_WEEK, weekCalendar.getFirstDayOfWeek());

		Date startDate = weekCalendar.getTime(); // 得到周的开始日期

		weekCalendar.roll(Calendar.DAY_OF_WEEK, 6);
		Date endDate = weekCalendar.getTime(); // 得到周的结束日期

		// 开始日期往前推一天
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(startDate);
		startCalendar.add(Calendar.DATE, 1);// 明天1，昨天-1
		startCalendar.set(Calendar.HOUR_OF_DAY, 0);
		startCalendar.set(Calendar.MINUTE, 0);
		startCalendar.set(Calendar.SECOND, 0);
		startCalendar.set(Calendar.MILLISECOND, 0);
		startDate = startCalendar.getTime();

		// 结束日期往前推一天
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		endCalendar.add(Calendar.DATE, 1);// 明天1，昨天-1
		endCalendar.set(Calendar.HOUR_OF_DAY, 23);
		endCalendar.set(Calendar.MINUTE, 59);
		endCalendar.set(Calendar.SECOND, 59);
		endCalendar.set(Calendar.MILLISECOND, 999);
		endDate = endCalendar.getTime();

		Map<String, Date> map = new HashMap<String, Date>();
		map.put("start", startDate);
		map.put("end", endDate);
		return map;
	}

	/**
	 * 根据日期月份，获取月份的开始和结束日期
	 * 
	 * @param date
	 * @return
	 */
	public static Map<String, Date> getMonthDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);

		// 得到前一个月的第一天
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		Date start = calendar.getTime();

		// 得到前一个月的最后一天
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		Date end = calendar.getTime();

		Map<String, Date> map = new HashMap<String, Date>();
		map.put("start", start);
		map.put("end", end);
		return map;
	}

	public static String getLastSqlTimestamp(String lastNum,String lastType){
		int time = Integer.valueOf(lastNum);
		time = - time;
		Calendar cl = Calendar.getInstance();
		cl.setTime(new Date());
		if("day".equals(lastType)){
			cl.add(Calendar.DATE, time);
		}
		else if("week".equals(lastType)){
			cl.add(Calendar.DATE, time*7);
		}else if("month".equals(lastType)){
			cl.add(Calendar.MONTH, time);
		}else if("year".equals(lastType)){
			cl.add(Calendar.YEAR,time);
		}else{
			throw new RuntimeException("最近时间类型输入错误");
		}
				
		return getSqlTimestamp(cl.getTime()).toString();
	
	}
	
	public static Date getLastDate(String lastNum,String lastType){
		int time = Integer.valueOf(lastNum);
		time = - time;
		Calendar cl = Calendar.getInstance();
		cl.setTime(new Date());
		if("day".equals(lastType)){
			cl.add(Calendar.DATE, time);
		}
		else if("week".equals(lastType)){
			cl.add(Calendar.DATE, time*7);
		}
		else if("month".equals(lastType)){
			cl.add(Calendar.MONTH, time);
		}
		else if("year".equals(lastType)){
			cl.add(Calendar.YEAR,time);
		}
		else{
			throw new RuntimeException("最近时间类型输入错误");
		}		
		return cl.getTime();
	}
	
	/**
	 * @Description 根据不含秒的字符串，获取完整的Timestamp格式时间
	 * @param date 页面上获取的时间字符串，不含秒
	 * @return 返回数据库类型时间Timestamp
	 */
	public static String getFullTime(String date){
		if(ToolValid.isNotNull(date)){
			return ToolDateTime.getSqlTimestampStr((date+":00"));
		}
		return null;
	}
	
	
	
	
	
	
	
	
	///////////////////////////////////////////////////////////////
	
	
	
	/**
	 * 将时间字符串格式化Date类型
	 * 
	 * @param str
	 * @param format
	 * @return Date
	 * @hint str,fromat 格式要一致
	 */
	public static Date convertStrToDate(String str, String format) {

		SimpleDateFormat fmt = new SimpleDateFormat(format);
		Date date = null;

		try {
			date = fmt.parse(str);

		} catch (Exception e) {
			date = null;
		}

		return date;
	}
	
	
	/**
	 * 毫秒转为时间
	 */
	public static String convertLongToDate(long Long, String format) {
		StringBuffer sb = new StringBuffer();
		Date d = new Date();
		d.setTime(Long);
		d = new Date(Long);
		sb.append(convertDateToStr(d, format));
		return sb.toString();
	}
	
	
	/**
	 * 将Date类型转为String类型
	 * 
	 * @param date
	 * @param format
	 * @return String
	 * @hint date,fromat 格式要一致
	 */
	public static String convertDateToStr(Date date, String format) {

		SimpleDateFormat fmt = new SimpleDateFormat(format, Locale.CHINA);
		String str = null;

		try {
			str = fmt.format(date);

		} catch (Exception e) {
			str = null;
		}

		return str;
	}
	
	
	/**
	 * 时间转成毫秒
	 */
	public static Long convertDateToLong(String str, String format){
		StringBuffer sb = new StringBuffer();
		
		if(!"".equals(str) && str.length()>0){
			Date _date = convertStrToDate(str, format);
			sb.append(_date.getTime());
		}
		
		return Long.parseLong(sb.toString());
	}
	
	
	
	/**
	 * @param myDate
	 * @param format
	 * @return 当前时间
	 * @hint myDate, format 格式要一致
	 * @caption G  &nbsp;&nbsp;&nbsp;公元 <br />
		Y  &nbsp;&nbsp;&nbsp;年 <br />
		M  &nbsp;&nbsp;&nbsp;月 <br />
		d  &nbsp;&nbsp;&nbsp;日 <br />
		h  &nbsp;&nbsp;&nbsp;A.M/P.M制的钟点(1-12) <br />
		H  &nbsp;&nbsp;&nbsp;24小时制的钟点(0-23) <br />
		m  &nbsp;&nbsp;&nbsp;分钟 <br />
		s  &nbsp;&nbsp;&nbsp;秒  <br />
		S  &nbsp;&nbsp;&nbsp;毫秒 <br />
		E  &nbsp;&nbsp;&nbsp;星期几 <br />
		D  &nbsp;&nbsp;&nbsp;一年中的第几天 <br />
		F  &nbsp;&nbsp;&nbsp;某月的第几个星期几 <br />
		w  &nbsp;&nbsp;&nbsp;一年中的第几个星期 <br />
		W  &nbsp;&nbsp;&nbsp;一个月中的第几个星期 <br />
		a  &nbsp;&nbsp;&nbsp;A.M/P.M标记 <br />
		k  &nbsp;&nbsp;&nbsp;24小时制的钟点(1-24)  <br />
		K  &nbsp;&nbsp;&nbsp;A.M/P.M制的钟点(0-11) <br />
		z  &nbsp;&nbsp;&nbsp;时区 

	 */
	public static String getNowDate(Date myDate, String format) {

		SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.CHINA);
		String strDate = null;

		try {
			strDate = formatter.format(myDate);

		} catch (Exception e) {
			strDate = null;
		}

		return strDate;
	}
	
	
	
	
	/**
	 * 取得num天/月/年以后的日期时间(时间基数为当前时间)
	 * 
	 * @param num
	 * @param format
	 * @param flag       Y: year, M: month, D: day, H: hour, f: 分
	 * @return String
	 */
	public static String getDate(int num, String format, char flag) {
		SimpleDateFormat fm = new SimpleDateFormat(format);
		String strDate = null;
		try {
			Calendar now = Calendar.getInstance();
			
			switch (flag) {
				case 'Y':
					now.add(Calendar.YEAR, num);
					break;
				case 'M':
					now.add(Calendar.MONTH, num);
					break;
				case 'D':
					now.add(Calendar.DAY_OF_MONTH, num);
					break;
				case 'H':
					now.add(Calendar.HOUR, num);
					break;
				case 'f':
					now.add(Calendar.MINUTE, num);
					break;
	
				default:
					break;
			}
			Date times = new Date();
			times = now.getTime();
			strDate = fm.format(times);

		} catch (Exception e) {
			strDate = null;
		}

		return strDate;
	}
	
	/**
	 * 取得num天/月/年以后的日期时间(含时间基数)
	 * 
	 * @param num
	 * @param format
	 * @param flag       Y: year, M: month, D: day, H: hour f: 分
	 * @return String
	 */
	public static String getDate(String baseDate, int num, String format, char flag) {
		SimpleDateFormat fm = new SimpleDateFormat(format);
		
		String strDate = null;
		try {
			
			Calendar now = Calendar.getInstance();
			
			if(!"".equals(baseDate) && baseDate.length()>0){
				now = parseDateTime(baseDate);
			}
			else{
				now = Calendar.getInstance();
			}
			switch (flag) {
			case 'Y':
				now.add(Calendar.YEAR, num);
				break;
			case 'M':
				now.add(Calendar.MONTH, num);
				break;
			case 'D':
				now.add(Calendar.DAY_OF_MONTH, num);
				break;
			case 'H':
				now.add(Calendar.HOUR, num);
				break;
			case 'f':
				now.add(Calendar.MINUTE, num);
				break;

			default:
				break;
			}
			Date times = new Date();
			times = now.getTime();
			strDate = fm.format(times);

		} catch (Exception e) {
			strDate = null;
		}
		return strDate;
	}
	
	
	/**
	 * 字符串时间转Calendar
	 * 
	 * @param baseDate
	 * @return
	 */
	public static Calendar parseDateTime(String baseDate) {
		Calendar cal = null;
		cal = new GregorianCalendar();
		int yy = Integer.parseInt(baseDate.substring(0, 4));
		int mm = Integer.parseInt(baseDate.substring(5, 7)) - 1;
		int dd = Integer.parseInt(baseDate.substring(8, 10));
		int hh = 0;
		int mi = 0;
		int ss = 0;
		if (baseDate.length() > 12) {
			hh = Integer.parseInt(baseDate.substring(11, 13));
			mi = Integer.parseInt(baseDate.substring(14, 16));
			ss = Integer.parseInt(baseDate.substring(17, 19));
		}
		cal.set(yy, mm, dd, hh, mi, ss);
		return cal;
	}
	
	
	/**
	 * 取得月份天数
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getDayNum(int year, int month) {

		int num = 0;
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			num = 31;
			break;
		case 2:
			if (year % 4 == 0) {
				num = 29;
			} 
			else {
				num = 28;
			}
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			num = 30;
			break;
		default:
			break;
		}

		return num;
	}
	
	
	/**
	 * @Title: getMonthFirstDay
	 * @Description: 获取当前月份的第一天
	 * @return String
	 */
	public static String getMonthFirstDay(){
		
		return getNowDate(new Date(), Constants.BASEDATEFROMATYYMM) + "-01";
	}
	
	
	/**
	 * @Title: getMonthFirstDay
	 * @Description: 获取指定月份的第一天
	 * @param year
	 * @param month
	 * @return String
	 */
	public static String getMonthFirstDay(String year, String month){
		
		return year + Constants.SPLIT_STRIPING + month + Constants.SPLIT_STRIPING + Constants.TIME_MONTHFIRSTDAY;
	}
	
	
	
	/**
	 * @Title: getMonthLastDay
	 * @Description: 获取当前月份的最后一天
	 * @return String
	 */
	public static String getMonthLastDay() {

		String[] time = getNowDate(new Date(), Constants.BASEDATEFROMATSIMPLE).split(Constants.SPLIT_STRIPING);

		return getNowDate(new Date(), Constants.BASEDATEFROMATYYMM) + Constants.SPLIT_STRIPING
				+ getDayNum(Integer.parseInt(time[0]), Integer.parseInt(time[1]));
	}
	
	
	
	/**
	 * @Title: getMonthLastDay
	 * @Description: 获取指定月份的最后一天
	 * @param year
	 * @param month
	 * @return String
	 */
	public static String getMonthLastDay(String year, String month) {

		return year + Constants.SPLIT_STRIPING + month + Constants.SPLIT_STRIPING
				+ getDayNum(Integer.parseInt(year), Integer.parseInt(month));
	}
	
	
	
	/**
	 * @Title: getSTimeAndETime
	 * @Description: 获取开始时间和结束时间
	 * @param month
	 * @param pStartTime
	 * @param pEndTime
	 * @return Map<String,Long>
	 */
	public static BetweenTime getSTimeAndETime(Long month, String pStartTime, String pEndTime){
		BetweenTime bt = new BetweenTime();
		long startTime = 0L;
		long endTime = 0L;
		
		if(null== month || 0 == month){
			bt = getQueryDefaultMonth();
		}
		else{
			if(null == pStartTime || "".equals(pStartTime)){
				startTime = getQueryAppointMonthStartTime(month);
				bt.setStartTime(startTime);
			}
			else{
				bt.setStartTime(new Long(pStartTime));
			}
			
			if(null == pEndTime || "".equals(pEndTime)){
				endTime = getQueryAppointMonthEndTime(month);
				bt.setEndTime(endTime);
			}
			else{
				bt.setEndTime(new Long(pEndTime));
			}
		}
		
		return bt;
	}
	
	
	public static BetweenTime getSTimeAndETime(Long month, Long pStartTime, Long pEndTime){
		BetweenTime bt = new BetweenTime();
		long startTime = 0L;
		long endTime = 0L;
		
		if(null==month || 0 == month){
			bt = getQueryDefaultMonth();
		}
		else{
			if(null == pStartTime){
				startTime = getQueryAppointMonthStartTime(month);
				bt.setStartTime(startTime);
			}
			else{
				bt.setStartTime(pStartTime);
			}
			
			if(null == pEndTime){
				endTime = getQueryAppointMonthEndTime(month);
				bt.setEndTime(endTime);
			}
			else{
				bt.setEndTime(pEndTime);
			}
		}
		
		return bt;
	}
	
	
	/**
	 * @Title: getQueryDefaultMonth
	 * @Description: 获取默认开始时间,结束时间
	 * @return Map<String,Long>
	 */
	public static BetweenTime getQueryDefaultMonth(){
		BetweenTime bt = new BetweenTime();
		String aTime = ToolDateTime.getMonthFirstDay();
		String eTime = ToolDateTime.getDate(aTime, 1, Constants.BASEDATEFROMATSIMPLE, 'M');
		Long st = ToolDateTime.convertDateToLong(aTime, Constants.BASEDATEFROMATSIMPLE) / 1000;
		Long et = ToolDateTime.convertDateToLong(eTime, Constants.BASEDATEFROMATSIMPLE) / 1000;
		bt.setStartTime(st);
		bt.setEndTime(et);
				
		return bt;
	}
	
	/**
	 * @Title: getQueryAppointMonthStartTime
	 * @Description: 获取指定月份开始时间
	 * @param month
	 * @return Long
	 */
	public static Long getQueryAppointMonthStartTime(long month){
		String[] time = ToolDateTime.convertLongToDate(month * 1000L, Constants.BASEDATEFROMATYYMM).split(Constants.SPLIT_STRIPING);
		
		String aTime = ToolDateTime.getMonthFirstDay(time[0], time[1]);
		Long st = ToolDateTime.convertDateToLong(aTime, Constants.BASEDATEFROMATSIMPLE) / 1000;
		
		return st;
	}
	
	
	/**
	 * @Title: currentTimeSecond
	 * @Description: 获取当前时间秒
	 * @return Long
	 */
	public static Long currentTimeSecond(){
		return System.currentTimeMillis() / 1000;
	}
	
	
	/**
	 * @Title: getQueryAppointMonthEndTime
	 * @Description: 获取指定月份结束时间
	 * @param month
	 * @return Long
	 */
	public static Long getQueryAppointMonthEndTime(long month){
		String[] time = ToolDateTime.convertLongToDate(month * 1000L, Constants.BASEDATEFROMATYYMM).split(Constants.SPLIT_STRIPING);
		
		String aTime = ToolDateTime.getMonthFirstDay(time[0], time[1]);
		String eTime = ToolDateTime.getDate(aTime, 1, Constants.BASEDATEFROMATSIMPLE, 'M');
		Long et = ToolDateTime.convertDateToLong(eTime, Constants.BASEDATEFROMATSIMPLE) / 1000;
		
		return et;
	}
	
	
	public static class BetweenTime {
		
		private Long startTime;
		private Long endTime;
		
		public Long getStartTime() {
			return startTime;
		}
		public void setStartTime(Long startTime) {
			this.startTime = startTime;
		}
		public Long getEndTime() {
			return endTime;
		}
		public void setEndTime(Long endTime) {
			this.endTime = endTime;
		}
		
	}
	
	
	/**
	 * 获取指定日期的开始时间结束时间
	 * 
	 * @return    参数
	 * @return BetweenTime    返回类型
	 */
    public static BetweenTime getSTimeAndETime(String nowDate) {
        BetweenTime bt = new BetweenTime();
        long startTime = convertDateToLong(nowDate + " 00:00:00", Constants.BASEDATEFROMAT) / 1000;
        long endTime = convertDateToLong(nowDate + " 23:59:59", Constants.BASEDATEFROMAT) / 1000;
        bt.setStartTime(startTime);
        bt.setEndTime(endTime);

        return bt;
    }
}



